package cmcciot.onenet.nbapi.sdk.entity;

import cmcciot.onenet.nbapi.sdk.config.Config;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Date;

/**
 * Created by zhuocongbin
 * date 2018/3/15
 */
public class Execute extends CommonEntity {
    private final int timeout;

    /**
     * @param imei                   设备IMEI号，必填
     * @param objId，下发命令的对象ID,必填
     * @param objInstId，下发命令的实例ID,必填
     * @param resId，下发命令的资源ID,必填
     */
    public Execute(String imei, Integer objId, Integer objInstId, Integer resId, Integer timeout) {
        this.imei = imei;
        this.objId = objId;
        this.objInstId = objInstId;
        this.resId = resId;
        this.timeout = timeout;
    }

    @Override
    public String toUrl() {
        if (timeout == 0) {
            return Config.getDomainName() + "/nbiot/execute?imei=" + this.imei +
                    "&obj_id=" + this.objId +
                    "&obj_inst_id=" + this.objInstId +
                    "&res_id=" + this.resId;
        } else {
            ZonedDateTime expiredTime = LocalDateTime.now().plusSeconds(timeout).atZone(ZoneOffset.UTC);
            return Config.getDomainName() + "/nbiot/execute/offline?imei=" + this.imei +
                    "&obj_id=" + this.objId +
                    "&obj_inst_id=" + this.objInstId +
                    "&res_id=" + this.resId +
                    "&expired_time=" + expiredTime;

        }
    }
}
